#include <stdio.h>
#include "mpi.h"
#include <omp.h>

const int num_steps = 2E9;

int main(int ac, char **av){
	int numprocess, myid;
	double step;
	double res, start, end;
	double sum = 0.0, pi;
	MPI_Init(&ac, &av);
	MPI_Comm_size(MPI_COMM_WORLD, &numprocess);
	MPI_Comm_rank(MPI_COMM_WORLD, &myid      );
	start = MPI_Wtime();
	if(myid == 0) 
		step = 1.0L / num_steps;
	MPI_Bcast(&step, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);

	for(int i = myid + 1;i <= num_steps; i+= numprocess){
		double x = (i - 0.5) * step;
		sum += 4.0L / (1.0L + x * x);
	} 
	MPI_Reduce(&sum, &pi, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);

	if(myid == 0){
		printf("pi = %.20lf\n", pi * step );
		end = MPI_Wtime();
		printf("Size = %d, rank = %d, time = %f \n", numprocess, myid,  (end - start));
	}


	MPI_Finalize();


}

